home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / pgp23src.zip / SRC / ZDEFLATE.C < prev    next >
C/C++ Source or Header  |  1993-05-09  |  27KB  |  710 lines

  1. /*
  2.  
  3.  Copyright (C) 1990-1992 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  4.  Kai Uwe Rommel and Igor Mandrichenko.
  5.  Permission is granted to any individual or institution to use, copy, or
  6.  redistribute this software so long as all of the original files are included
  7.  unmodified, that it is not sold for profit, and that this copyright notice
  8.  is retained.
  9.  
  10. */
  11.  
  12. /*
  13.  *  deflate.c by Jean-loup Gailly.
  14.  *
  15.  *  PURPOSE
  16.  *
  17.  *      Identify new text as repetitions of old text within a fixed-
  18.  *      length sliding window trailing behind the new text.
  19.  *
  20.  *  DISCUSSION
  21.  *
  22.  *      The "deflation" process depends on being able to identify portions
  23.  *      of the input text which are identical to earlier input (within a
  24.  *      sliding window trailing behind the input currently being processed).
  25.  *
  26.  *      The most straightforward technique turns out to be the fastest for
  27.  *      most input files: try all possible matches and select the longest.
  28.  *      The key feature is of this algorithm is that insertion and deletions
  29.  *      from the string dictionary are very simple and thus fast. Insertions
  30.  *      and deletions are performed at each input character, whereas string
  31.  *      matches are performed only when the previous match ends. So it is
  32.  *      preferable to spend more time in matches to allow very fast string
  33.  *      insertions and deletions. The matching algorithm for small strings
  34.  *      is inspired from that of Rabin & Karp. A brute force approach is
  35.  *      used to find longer strings when a small match has been found.
  36.  *      A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
  37.  *      (by Leonid Broukhis).
  38.  *         A previous version of this file used a more sophisticated algorithm
  39.  *      (by Fiala and Greene) which is guaranteed to run in linear amortized
  40.  *      time, but has a larger average cost and uses more memory. However
  41.  *      the F&G algorithm may be faster for some highly redundant files if
  42.  *      the parameter max_chain_length (described below) is too large.
  43.  *
  44.  *  ACKNOWLEDGEMENTS
  45.  *
  46.  *      The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
  47.  *      I found it in 'freeze' written by Leonid Broukhis.
  48.  *      Thanks to many info-zippers for bug reports and testing.
  49.  *
  50.  *  REFERENCES
  51.  *
  52.  *      APPNOTE.TXT documentation file in PKZIP 2.0 distribution.
  53.  *
  54.  *      A description of the Rabin and Karp algorithm is given in the book
  55.  *         "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
  56.  *
  57.  *      Fiala,E.R., and Greene,D.H.
  58.  *         Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
  59.  *
  60.  *  INTERFACE
  61.  *
  62.  *      void lm_init (int pack_level, ush *flags)
  63.  *          Initialize the "longest match" routines for a new file
  64.  *
  65.  *      ulg deflate (void)
  66.  *          Processes a new input file and return its compressed length. Sets
  67.  *          the compressed length, crc, deflate flags and internal file
  68.  *          attributes.
  69.  */
  70.  
  71. #include "zunzip.h"
  72. #include "zip.h"
  73.  
  74. /* ===========================================================================
  75.  * Configuration parameters
  76.  */
  77.  
  78. /* Compile with MEDIUM_MEM to reduce the memory requirements or
  79.  * with SMALL_MEM to use as little memory as possible.
  80.  * Warning: defining these symbols affects MATCH_BUFSIZE and HASH_BITS
  81.  * (see below) and thus affects the compression ratio. The compressed output
  82.  * is still correct, and might even be smaller in some cases.
  83.  */
  84.  
  85. #ifdef SMALL_MEM
  86. #   define HASH_BITS  13  /* Number of bits used to hash strings */
  87. #else
  88. #ifdef MEDIUM_MEM
  89. #   define HASH_BITS  14
  90. #else
  91. #   define HASH_BITS  15
  92.    /* For portability to 16 bit machines, do not use values above 15. */
  93. #endif
  94. #endif
  95.  
  96. #define HASH_SIZE (unsigned)(1<<HASH_BITS)
  97. #define HASH_MASK (HASH_SIZE-1)
  98. #define WMASK     (WSIZE-1)
  99. /* HASH_SIZE and WSIZE must be powers of two */
  100.  
  101. #define NIL 0
  102. /* Tail of hash chains */
  103.  
  104. #define FAST 4
  105. #define SLOW 2
  106. /* speed options for the general purpose bit flag */
  107.  
  108. #ifndef TOO_FAR
  109. #  define TOO_FAR 4096
  110. #endif
  111. /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
  112.  
  113. /* ===========================================================================
  114.  * Local data used by the "longest match" routines.
  115.  */
  116.  
  117. typedef ush Pos;
  118. typedef unsigned IPos;
  119. /* A Pos is an index in the character window. We use short instead of int to
  120.  * save space in the various tables. IPos is used only for parameter passing.
  121.  */
  122.  
  123. #ifndef DYN_ALLOC
  124.   uch    far window[2L*WSIZE];
  125.   /* Sliding window. Input bytes are read into the second half of the window,
  126.    * and move to the first half later to keep a dictionary of at least WSIZE
  127.    * bytes. With this organization, matches are limited to a distance of
  128.    * WSIZE-MAX_MATCH bytes, but this ensures that IO is always
  129.    * performed with a length multiple of the block size. Also, it limits
  130.    * the window size to 64K, which is quite useful on MSDOS.
  131.    * To do: limit the window size to WSIZE+BSZ if SMALL_MEM (the code would
  132.    * be less efficient since the data would have to be copied WSIZE/BSZ times)
  133.    */
  134.   Pos    far prev[WSIZE];
  135.   /* Link to older string with same hash index. To limit the size of this
  136.    * array to 64K, this link is maintained only for the last 32K strings.
  137.    * An index in this array is thus a window index modulo 32K.
  138.    */
  139.   Pos    far head[HASH_SIZE];
  140.   /* Heads of the hash chains or NIL */
  141. #else
  142.   uch    far * near window = NULL;
  143.   Pos    far * near prev   = NULL;
  144.   static void far *__window, *__prev;
  145.   static Pos    far * near head;
  146. #endif
  147.  
  148. long block_start;
  149. /* window position at the beginning of the current output block. Gets
  150.  * negative when the window is moved backwards.
  151.  */
  152.  
  153. local unsigned near ins_h;  /* hash index of string to be inserted */
  154.  
  155. #define H_SHIFT  ((HASH_BITS+MIN_MATCH-1)/MIN_MATCH)
  156. /* Number of bits by which ins_h and del_h must be shifted at each
  157.  * input step. It must be such that after MIN_MATCH steps, the oldest
  158.  * byte no longer takes part in the hash key, that is:
  159.  *   H_SHIFT * MIN_MATCH >= HASH_BITS
  160.  */
  161.  
  162. unsigned int near prev_length;
  163. /* Length of the best match at previous step. Matches not greater than this
  164.  * are discarded. This is used in the lazy match evaluation.
  165.  */
  166.  
  167.       unsigned near strstart;      /* start of string to insert */
  168. unsigned near match_start;         /* start of matching string */
  169. local int      near eofile;        /* flag set at end of input file */
  170. local unsigned near lookahead;     /* number of valid bytes ahead in window */
  171.  
  172. unsigned near max_chain_length;
  173. /* To speed up deflation, hash chains are never searched beyond this length.
  174.  * A higher limit improves compression ratio but degrades the speed.
  175.  */
  176.  
  177. local unsigned int max_lazy_match;
  178. /* Attempt to find a better match only when the current match is strictly
  179.  * smaller than this value.
  180.  */
  181.  
  182. int near good_match;
  183. /* Use a faster search when the previous match is longer than this */
  184.  
  185.  
  186. /* Values for max_lazy_match, good_match and max_chain_length, depending on
  187.  * the desired pack level (0..9). The values given below have been tuned to
  188.  * exclude worst case performance for pathological files. Better values may be
  189.  * found for specific files.
  190.  */
  191. typedef struct config {
  192.    int good_length;
  193.    int max_lazy;
  194.    unsigned max_chain;
  195.    uch flag;
  196. } config;
  197.  
  198. local config configuration_table[10] = {
  199. /*      good lazy chain flag */
  200. /* 0 */ {0,    0,    0,  0},     /* store only */
  201. /* 1 */ {4,    4,   16,  FAST},  /* maximum speed  */
  202. /* 2 */ {6,    8,   16,  0},
  203. /* 3 */ {8,   16,   32,  0},
  204. /* 4 */ {8,   32,   64,  0},
  205. /* 5 */ {8,   64,  128,  0},
  206. /* 6 */ {8,  128,  256,  0},
  207. /* 7 */ {8,  128,  512,  0},
  208. /* 8 */ {32, 258, 1024,  0},
  209. /* 9 */ {32, 258, 4096,  SLOW}}; /* maximum compression */
  210.  
  211. /* Note: the current code requires max_lazy >= MIN_MATCH and max_chain >= 4
  212.  * but these restrictions can easily be removed at a small cost.
  213.  */
  214.  
  215. #define EQUAL 0
  216. /* result of memcmp for equal strings */
  217.  
  218. /* ===========================================================================
  219.  *  Prototypes for local functions. Use asm version by default for
  220.  *  MSDOS but not Unix. However the asm version version is recommended
  221.  *  for 386 Unix.
  222.  */
  223. #ifdef ATARI_ST
  224. #  undef MSDOS /* avoid the processor specific parts */
  225. #endif
  226. #if defined(MSDOS) && !defined(NO_ASM) && !defined(ASM)
  227. #  define ASM
  228. #endif
  229.  
  230. local void fill_window   OF((void));
  231.       int  longest_match OF((IPos cur_match));
  232. #ifdef ASM
  233.       void match_init OF((void)); /* asm code initialization */
  234. #endif
  235.  
  236. #ifdef DEBUG
  237. local  void check_match OF((IPos start, IPos match, int length));
  238. #endif
  239.  
  240. #undef MIN
  241. #define MIN(a,b) ((a) <= (b) ? (a) : (b))
  242. /* The arguments must not have side effects. */
  243.  
  244. /* ===========================================================================
  245.  * Update a hash value with the given input byte
  246.  * IN  assertion: all calls to to UPDATE_HASH are made with consecutive
  247.  *    input characters, so that a running hash key can be computed from the
  248.  *    previous key instead of complete recalculation each time.
  249.  */
  250. #define UPDATE_HASH(h,c) (h = (((h)<<H_SHIFT) ^ (c)) & HASH_MASK)
  251.  
  252. /* ===========================================================================
  253.  * Insert string s in the dictionary and set match_head to the previous head
  254.  * of the hash chain (the most recent string with same hash key). Return
  255.  * the previous length of the hash chain.
  256.  * IN  assertion: all calls to to INSERT_STRING are made with consecutive
  257.  *    input characters and the first MIN_MATCH bytes of s are valid
  258.  *    (except for the last MIN_MATCH-1 bytes of the input file).
  259.  */
  260. #define INSERT_STRING(s, match_head) \
  261.    (UPDATE_HASH(ins_h, window[(s) + MIN_MATCH-1]), \
  262.     prev[(s) & WMASK] = match_head = head[ins_h], \
  263.     head[ins_h] = (s))
  264.  
  265. /* ===========================================================================
  266.  * Initialize the "longest match" routines for a new file
  267.  */
  268. void lm_init (pack_level, flags)
  269.     int pack_level; /* 0: store, 1: best speed, 9: best compression */
  270.     ush *flags;     /* general purpose bit flag */
  271. {
  272.     register unsigned j;
  273.  
  274.     if (pack_level < 1 || pack_level > 9) error("bad pack level");
  275.  
  276.     /* Use dynamic allocation if compiler does not like big static arrays: */
  277. #ifdef DYN_ALLOC
  278.     __window = window = (uch far*) fcalloc(WSIZE*2*sizeof(uch)+16, 1);
  279.     __prev = prev = (Pos far*) fcalloc(WSIZE*sizeof(Pos)+16, 1);
  280.     head   = (Pos far*) fcalloc(HASH_SIZE, sizeof(Pos));
  281.  
  282.     if (window == NULL || prev == NULL || head == NULL) {
  283.         err(ZE_MEM, "window allocation");
  284.     }
  285. #endif /* DYN_ALLOC */
  286. #ifdef ASM
  287.     match_init(); /* initialize the asm code */
  288. #endif
  289.     /* Initialize the hash table. */
  290.     for (j = 0;  j < HASH_SIZE; j++) head[j] = NIL;
  291.     /* prev will be initialized on the fly */
  292.  
  293.     /* Set the default configuration parameters:
  294.      */
  295.     max_lazy_match   = configuration_table[pack_level].max_lazy;
  296.     good_match       = configuration_table[pack_level].good_length;
  297.     max_chain_length = configuration_table[pack_level].max_chain;
  298.     *flags          |= configuration_table[pack_level].flag;
  299.     /* ??? reduce max_chain_length for binary files */
  300.  
  301.     strstart = 0;
  302.     block_start = 0L;
  303.  
  304. #if defined(MSDOS) && !defined(__32BIT__)
  305.     /* Can't read a 64K block under MSDOS */
  306.     lookahead = read_buf((char*)window, (unsigned)WSIZE);
  307. #else
  308.     lookahead = read_buf((char*)window, 2*WSIZE);
  309. #endif
  310.     if (lookahead == 0 || lookahead == (unsigned)EOF) {
  311.        eofile = 1, lookahead = 0;
  312.        return;
  313.     }
  314.     eofile = 0;
  315.     /* Make sure that we always have enough lookahead. This is important
  316.      * if input comes from a device such as a tty.
  317.      */
  318.     while (lookahead < MIN_LOOKAHEAD && !eofile) fill_window();
  319.  
  320.     ins_h = 0;
  321.     for (j=0; j<MIN_MATCH-1; j++) UPDATE_HASH(ins_h, window[j]);
  322.     /* If lookahead < MIN_MATCH, ins_h is garbage, but this is
  323.      * not important since only literal bytes will be emitted.
  324.      */
  325. }
  326.  
  327. void lm_free()
  328. {
  329. #ifdef DYN_ALLOC
  330.     free(__window);
  331.     free(__prev);
  332.     free(head);
  333.     window = NULL;
  334.     prev = head = NULL;
  335. #endif
  336. }
  337.  
  338. /* ===========================================================================
  339.  * Set match_start to the longest match starting at the given string and
  340.  * return its length. Matches shorter or equal to prev_length are discarded,
  341.  * in which case the result is equal to prev_length and match_start is
  342.  * garbage.
  343.  * IN assertions: cur_match is the head of the hash chain for the current
  344.  *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  345.  */
  346. #ifndef ASM
  347. /* For MSDOS, OS/2 and 386 Unix, an optimized version is in match.asm. The code
  348.  * is functionally equivalent, so you can use the C version if desired.
  349.  */
  350. int longest_match(cur_match)
  351.     IPos cur_match;                             /* current match */
  352. {
  353.     unsigned chain_length = max_chain_length;   /* max hash chain length */
  354.     register uch far *scan = window + strstart; /* current string */
  355.     register uch far *match = scan;             /* matched string */
  356.     register int len;                           /* length of current match */
  357.     int best_len = prev_length;                 /* best match length so far */
  358.     IPos limit = strstart > (IPos)MAX_DIST ? strstart - (IPos)MAX_DIST : NIL;
  359.     /* Stop when cur_match becomes <= limit. To simplify the code,
  360.      * we prevent matches with the string of window index 0.
  361.      */
  362. #ifdef UNALIGNED_OK
  363.     register ush scan_start = *(ush*)scan;
  364.     register ush scan_end   = *(ush*)(scan+best_len-1);
  365. #else
  366.     register uch scan_start = *scan;
  367.     register uch scan_end1  = scan[best_len-1];
  368.     register uch scan_end   = scan[best_len];
  369. #endif
  370.  
  371.     /* Do not waste too much time if we already have a good match: */
  372.     if (prev_length >= good_match) {
  373.         chain_length >>= 2;
  374.     }
  375.  
  376.     do {
  377.         Assert(cur_match < strstart, "no future");
  378.         match = window + cur_match;
  379.  
  380.         /* Skip to next match if the match length cannot increase
  381.          * or if the match length is less than 2:
  382.          */
  383. #if (defined(UNALIGNED_OK) && HASH_BITS >= 8)
  384.         /* This code assumes sizeof(unsigned short) == 2 and
  385.          * sizeof(unsigned long) == 4. Do not use UNALIGNED_OK if your
  386.          * compiler uses different sizes.
  387.          */
  388.         if (*(ush*)(match+best_len-1) != scan_end ||
  389.             *(ush*)match != scan_start) continue;
  390.  
  391.         len = MIN_MATCH - 4;
  392.         /* It is not necessary to compare scan[2] and match[2] since they are
  393.          * always equal when the other bytes match, given that the hash keys
  394.          * are equal and that HASH_BITS >= 8.
  395.          */
  396.         do {} while ((len+=4) < MAX_MATCH-3 &&
  397.                      *(ulg*)(scan+len) == *(ulg*)(match+len));
  398.         /* The funny do {} generates better code for most compilers */
  399.  
  400.         if (*(ush*)(scan+len) == *(ush*)(match+len)) len += 2;
  401.         if (scan[len] == match[len]) len++;
  402.  
  403. #else /* UNALIGNED_OK */
  404.         if (match[best_len] != scan_end ||
  405.             match[best_len-1] != scan_end1 || *match != scan_start)
  406.            continue;
  407.         /* It is not necessary to compare scan[1] and match[1] since they
  408.          * are always equal when the other bytes match, given that
  409.          * the hash keys are equal and that h_shift+8 <= HASH_BITS,
  410.          * that is, when the last byte is entirely included in the hash key.
  411.          * The condition is equivalent to
  412.          *       (HASH_BITS+2)/3 + 8 <= HASH_BITS
  413.          * or: HASH_BITS >= 13
  414.          * Also, we check for a match at best_len-1 to get rid quickly of
  415.          * the match with the suffix of the match made at the previous step,
  416.          * which is known to fail.
  417.          */
  418. #if HASH_BITS >= 13
  419.         len = 1;
  420. #else
  421.         len = 0;
  422. #endif
  423.         do {} while (++len < MAX_MATCH && scan[len] == match[len]);
  424.  
  425. #endif /* UNALIGNED_OK */
  426.  
  427.         if (len > best_len) {
  428.             match_start = cur_match;
  429.             best_len = len;
  430.             if (len == MAX_MATCH) break;
  431. #ifdef UNALIGNED_OK
  432.             scan_end = *(ush*)(scan+best_len-1);
  433. #else
  434.             scan_end1  = scan[best_len-1];
  435.             scan_end   = scan[best_len];
  436. #endif
  437.         }
  438.     } while (--chain_length != 0 &&
  439.              (cur_match = prev[cur_match & WMASK]) > limit);
  440.  
  441.     return best_len;
  442. }
  443. #endif /* NO_ASM */
  444.  
  445. #ifdef DEBUG
  446. /* ===========================================================================
  447.  * Check that the match at match_start is indeed a match.
  448.  */
  449. local void check_match(start, match, length)
  450.     IPos start, match;
  451.     int length;
  452. {
  453.     /* check that the match is indeed a match */
  454.     if (memcmp((char*)window + match,
  455.                 (char*)window + start, length) != EQUAL) {
  456.         fprintf(stderr,
  457.             " start %d, match %d, length %d\n",
  458.             start, match, length);
  459.         error("invalid match");
  460.     }
  461.     if (verbose > 1) {
  462.         fprintf(stderr,"\\[%d,%d]", start-match, length);
  463.         do { putc(window[start++], stderr); } while (--length != 0);
  464.     }
  465. }
  466. #else
  467. #  define check_match(start, match, length)
  468. #endif
  469.  
  470. /* ===========================================================================
  471.  * Fill the window when the lookahead becomes insufficient.
  472.  * Updates strstart and lookahead, and sets eofile if end of input file.
  473.  * IN assertion: lookahead < MIN_LOOKAHEAD && strstart + lookahead > 0
  474.  * OUT assertions: at least one byte has been read, or eofile is set;
  475.  *    file reads are performed for at least two bytes (required for the
  476.  *    translate_eol option).
  477.  */
  478. local void fill_window()
  479. {
  480.     register unsigned n, m;
  481.     unsigned more = (unsigned)((ulg)2*WSIZE - (ulg)lookahead - (ulg)strstart);
  482.     /* Amount of free space at the end of the window. */
  483.  
  484.     /* If the window is full, move the upper half to the lower one to make
  485.      * room in the upper half.
  486.      */
  487.     if (more == (unsigned)EOF) {
  488.         /* Very unlikely, but possible on 16 bit machine if strstart == 0
  489.          * and lookahead == 1 (input done one byte at time)
  490.          */
  491.         more--;
  492.     } else if (more <= 1) {
  493.         /* By the IN assertion, the window is not empty so we can't confuse
  494.          * more == 0 with more == 64K on a 16 bit machine.
  495.          */
  496.         memcpy((char*)window, (char*)window+WSIZE, (unsigned)WSIZE);
  497.         match_start -= WSIZE;
  498.         strstart    -= WSIZE;
  499.         /* strstart - WSIZE >= WSIZE - 1 - lookahead >= WSIZE - MIN_LOOKAHEAD
  500.          * so we now have strstart >= MAX_DIST:
  501.          */
  502.         Assert (strstart >= MAX_DIST, "window slide too early");
  503.         block_start -= (long) WSIZE;
  504.  
  505.         for (n = 0; n < HASH_SIZE; n++) {
  506.             m = head[n];
  507.             head[n] = (Pos)(m >= WSIZE ? m-WSIZE : NIL);
  508.         }
  509.         for (n = 0; n < WSIZE; n++) {
  510.             m = prev[n];
  511.             prev[n] = (Pos)(m >= WSIZE ? m-WSIZE : NIL);
  512.             /* If n is not on any hash chain, prev[n] is garbage but
  513.              * its value will never be used.
  514.              */
  515.         }
  516.         more += WSIZE;
  517. #ifdef ZIP
  518.         if (verbose) putc('.', stderr);
  519. #endif
  520.     }
  521.     /* At this point, more >= 2 */
  522.     n = read_buf((char*)window+strstart+lookahead, more);
  523.     if (n == 0 || n == (unsigned)EOF) {
  524.         eofile = 1;
  525.     } else {
  526.         lookahead += n;
  527.     }
  528. }
  529.  
  530. /* ===========================================================================
  531.  * Flush the current block, with given end-of-file flag.
  532.  * IN assertion: strstart is set to the end of the current match.
  533.  */
  534. #define FLUSH_BLOCK(eof) \
  535.    flush_block(block_start >= 0L ? (char*)&window[block_start] : (char*)NULL,\
  536.                (long)strstart - block_start, (eof))
  537.  
  538. /* ===========================================================================
  539.  * Processes a new input file and return its compressed length.
  540.  */
  541. #ifdef NO_LAZY
  542. ulg deflate()
  543. {
  544.     IPos hash_head; /* head of the hash chain */
  545.     int flush;      /* set if current block must be flushed */
  546.     unsigned match_length = 0;  /* length of best match */
  547.  
  548.     prev_length = MIN_MATCH-1;
  549.     while (lookahead != 0) {
  550.         /* Insert the string window[strstart .. strstart+2] in the
  551.          * dictionary, and set hash_head to the head of the hash chain:
  552.          */
  553.         INSERT_STRING(strstart, hash_head);
  554.  
  555.         /* Find the longest match, discarding those <= prev_length.
  556.          * At this point we have always match_length < MIN_MATCH
  557.          */
  558.         if (hash_head != NIL && strstart - hash_head <= MAX_DIST) {
  559.             /* To simplify the code, we prevent matches with the string
  560.              * of window index 0 (in particular we have to avoid a match
  561.              * of the string with itself at the start of the input file).
  562.              */
  563.             match_length = longest_match (hash_head);
  564.             /* longest_match() sets match_start */
  565.             if (match_length > lookahead) match_length = lookahead;
  566.         }
  567.         if (match_length >= MIN_MATCH) {
  568.             check_match(strstart, match_start, match_length);
  569.  
  570.             flush = ct_tally(strstart-match_start, match_length - MIN_MATCH);
  571.  
  572.             lookahead -= match_length;
  573.             match_length--; /* string at strstart already in hash table */
  574.             do {
  575.                 strstart++;
  576.                 INSERT_STRING(strstart, hash_head);
  577.                 /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  578.                  * always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH
  579.                  * these bytes are garbage, but it does not matter since the
  580.                  * next lookahead bytes will always be emitted as literals.
  581.                  */
  582.             } while (--match_length != 0);
  583.         } else {
  584.             /* No match, output a literal byte */
  585.             flush = ct_tally (0, window[strstart]);
  586.             lookahead--;
  587.         }
  588.         strstart++; 
  589.         if (flush) FLUSH_BLOCK(0), block_start = strstart;
  590.  
  591.         /* Make sure that we always have enough lookahead, except
  592.          * at the end of the input file. We need MAX_MATCH bytes
  593.          * for the next match, plus MIN_MATCH bytes to insert the
  594.          * string following the next match.
  595.          */
  596.         while (lookahead < MIN_LOOKAHEAD && !eofile) fill_window();
  597.  
  598.     }
  599.     return FLUSH_BLOCK(1); /* eof */
  600. }
  601. #else /* LAZY */
  602. /* ===========================================================================
  603.  * Same as above, but achieves better compression. We use a lazy
  604.  * evaluation for matches: a match is finally adopted only if there is
  605.  * no better match at the next window position.
  606.  */
  607. ulg deflate()
  608. {
  609.     IPos hash_head;          /* head of hash chain */
  610.     IPos prev_match;         /* previous match */
  611.     int flush;               /* set if current block must be flushed */
  612.     int match_available = 0; /* set if previous match exists */
  613.     register unsigned match_length = MIN_MATCH-1; /* length of best match */
  614. #ifdef DEBUG
  615.     extern ulg isize;        /* byte length of input file, for debug only */
  616. #endif
  617.  
  618.     /* Process the input block. */
  619.     while (lookahead != 0) {
  620.         /* Insert the string window[strstart .. strstart+2] in the
  621.          * dictionary, and set hash_head to the head of the hash chain:
  622.          */
  623.         INSERT_STRING(strstart, hash_head);
  624.  
  625.         /* Find the longest match, discarding those <= prev_length.
  626.          */
  627.         prev_length = match_length, prev_match = match_start;
  628.         match_length = MIN_MATCH-1;
  629.  
  630.         if (hash_head != NIL && prev_length < max_lazy_match &&
  631.             strstart - hash_head <= MAX_DIST) {
  632.             /* To simplify the code, we prevent matches with the string
  633.              * of window index 0 (in particular we have to avoid a match
  634.              * of the string with itself at the start of the input file).
  635.              */
  636.             match_length = longest_match (hash_head);
  637.             /* longest_match() sets match_start */
  638.             if (match_length > lookahead) match_length = lookahead;
  639.             /* Ignore a length 3 match if it is too distant: */
  640.             if (match_length == MIN_MATCH && strstart-match_start > TOO_FAR){
  641.                 /* If prev_match is also MIN_MATCH, match_start is garbage
  642.                  * but we will ignore the current match anyway.
  643.                  */
  644.                 match_length--;
  645.             }
  646.         }
  647.         /* If there was a match at the previous step and the current
  648.          * match is not better, output the previous match:
  649.          */
  650.         if (prev_length >= MIN_MATCH && match_length <= prev_length) {
  651.  
  652.             check_match(strstart-1, prev_match, prev_length);
  653.  
  654.             flush = ct_tally(strstart-1-prev_match, prev_length - MIN_MATCH);
  655.  
  656.             /* Insert in hash table all strings up to the end of the match.
  657.              * strstart-1 and strstart are already inserted.
  658.              */
  659.             lookahead -= prev_length-1;
  660.             prev_length -= 2;
  661.             do {
  662.                 strstart++;
  663.                 INSERT_STRING(strstart, hash_head);
  664.                 /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  665.                  * always MIN_MATCH bytes ahead. If lookahead < MIN_MATCH
  666.                  * these bytes are garbage, but it does not matter since the
  667.                  * next lookahead bytes will always be emitted as literals.
  668.                  */
  669.             } while (--prev_length != 0);
  670.             match_available = 0;
  671.             match_length = MIN_MATCH-1;
  672.             strstart++;
  673.             if (flush) FLUSH_BLOCK(0), block_start = strstart;
  674.  
  675.         } else if (match_available) {
  676.             /* If there was no match at the previous position, output a
  677.              * single literal. If there was a match but the current match
  678.              * is longer, truncate the previous match to a single literal.
  679.              */
  680.             Tracevv((stderr,"%c",window[strstart-1]));
  681.             if (ct_tally (0, window[strstart-1])) {
  682.                 FLUSH_BLOCK(0), block_start = strstart;
  683.             }
  684.             strstart++;
  685.             lookahead--;
  686.         } else {
  687.             /* There is no previous match to compare with, wait for
  688.              * the next step to decide.
  689.              */
  690.             match_available = 1;
  691.             strstart++;
  692.             lookahead--;
  693.         }
  694. #if 0    /* for pgp: disabled to allow compiling with -DDEBUG */
  695.         Assert (strstart <= isize && lookahead <= isize, "a bit too far");
  696. #endif
  697.  
  698.         /* Make sure that we always have enough lookahead, except
  699.          * at the end of the input file. We need MAX_MATCH bytes
  700.          * for the next match, plus MIN_MATCH bytes to insert the
  701.          * string following the next match.
  702.          */
  703.         while (lookahead < MIN_LOOKAHEAD && !eofile) fill_window();
  704.     }
  705.     if (match_available) ct_tally (0, window[strstart-1]);
  706.  
  707.     return FLUSH_BLOCK(1); /* eof */
  708. }
  709. #endif /* LAZY */
  710.